﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class TeeConnOperation : ConnOperation
	{
		public TeeConnOperation(UIDocument uiDoc, bool connPointSel) : base(uiDoc)
		{
			this.connPointSel = connPointSel;
		}

		public override bool SelectDucts()
		{
			if (!this.connPointSel)
			{
				bool flag = false;
				if (this.SelectDuctsMethod != null)
				{
					string errorString = "";
					IList<Duct> source = null;
					IList<XYZ> source2 = null;
					ConnectDuctStyle fittingStyle = this.fittingStyle;
					switch (fittingStyle)
					{
					case ConnectDuctStyle.TShapeTee:
					case ConnectDuctStyle.YShapeTee:
						flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.TeeFConn, ref source, ref source2, ref errorString);
						goto IL_270;
					case ConnectDuctStyle.RoundAngleTee:
					case ConnectDuctStyle.RightAngleTee:
					case ConnectDuctStyle.IntubationTee:
						break;
					case ConnectDuctStyle.RReduceAngleTee:
						goto IL_270;
					case ConnectDuctStyle.CommonTee:
						goto IL_25B;
					default:
						switch (fittingStyle)
						{
						case ConnectDuctStyle.RightAngleTeeRound:
							break;
						case ConnectDuctStyle.SlantingTeeRound:
							flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.TeeXConn, ref source, ref source2, ref errorString);
							goto IL_270;
						case ConnectDuctStyle.CommonTeeRound:
							goto IL_25B;
						default:
							goto IL_270;
						}
						break;
					}
					flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.TeeTConn, ref source, ref source2, ref errorString);
					goto IL_270;
					IL_25B:
					flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.TeeYConn, ref source, ref source2, ref errorString);
					IL_270:
					if (flag)
					{
						base.SelectedDucts = source.ToList<Duct>();
						base.SelectedPoints = source2.ToList<XYZ>();
					}
					else
					{
						base.ErrorString = errorString;
					}
				}
				return flag;
			}
			ISelectionFilter selectionFilter = new DuctFilter();
			Reference reference = base.UIDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第一根风管……");
			base.SelectedDucts.Add(base.UIDoc.Document.GetElement(reference) as Duct);
			base.SelectedPoints.Add(reference.GlobalPoint);
			new HYDuctConnect();
			int ductShape = new ConnectDuctManage().GetDuctShape(base.SelectedDucts[0]);
			if (base.SelectedDucts.Count < 1)
			{
				base.ErrorString = "没有选中风管，请选择一根风管！";
				return false;
			}
			Reference reference2 = base.UIDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择出口风管");
			base.SelectedDucts.Add(base.UIDoc.Document.GetElement(reference2) as Duct);
			base.SelectedPoints.Add(reference2.GlobalPoint);
			reference2 = base.UIDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择支管");
			base.SelectedDucts.Add(base.UIDoc.Document.GetElement(reference2) as Duct);
			base.SelectedPoints.Add(reference2.GlobalPoint);
			if (!base.CheckDuctShape(base.SelectedDucts))
			{
				base.ErrorString = "选择的三根风管的截面类型必须相同！";
				return false;
			}
			if ((this.fittingStyle == ConnectDuctStyle.RoundAngleTee || this.fittingStyle == ConnectDuctStyle.RightAngleTee || this.fittingStyle == ConnectDuctStyle.IntubationTee || this.fittingStyle == ConnectDuctStyle.TShapeTee || this.fittingStyle == ConnectDuctStyle.YShapeTee || this.fittingStyle == ConnectDuctStyle.CommonTee) && ductShape != 1)
			{
				base.ErrorString = "三通截面与风管截面不匹配！";
				return false;
			}
			if ((this.fittingStyle == ConnectDuctStyle.CommonTeeRound || this.fittingStyle == ConnectDuctStyle.RightAngleTeeRound || this.fittingStyle == ConnectDuctStyle.SlantingTeeRound) && ductShape != 0)
			{
				base.ErrorString = "三通截面与风管截面不匹配！";
				return false;
			}
			return true;
		}

		public override bool Connect()
		{
			bool result = false;
			if (base.SelectedDucts.Count < 1)
			{
				base.ErrorString = "选择的风管个数不对！";
				return result;
			}
			HYDuctConnect hyductConnect = new HYDuctConnect();
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			if (!connectDuctManage.BeSameShapeDucts(base.SelectedDucts))
			{
				base.ErrorString = "选择的风管截面必须一致！";
				return result;
			}
			int ductShape = connectDuctManage.GetDuctShape(base.SelectedDucts[0]);
			FamilyInstance familyInstance = null;
			string empty = string.Empty;
			List<string> list = new List<string>();
			if (base.ConnForm.Flag)
			{
				FamilySymbol familySymbol = base.UIDoc.Document.GetElementById(base.SelectedDucts[0].DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)2, 0).MEPPartId) as FamilySymbol;
				if (familySymbol == null)
				{
					base.ErrorString = "没有可以使用的族配置，请检查Revit的族配置";
					return result;
				}
				hyductConnect.NewTeeFittingY(base.SelectedDucts, base.SelectedPoints, familySymbol);
				return true;
			}
			else
			{
				if ((this.fittingStyle == ConnectDuctStyle.IntubationTee || this.fittingStyle == ConnectDuctStyle.YShapeTee || this.fittingStyle == ConnectDuctStyle.TShapeTee || this.fittingStyle == ConnectDuctStyle.CommonTee || this.fittingStyle == ConnectDuctStyle.RoundAngleTee || this.fittingStyle == ConnectDuctStyle.RightAngleTee) && ductShape != 1)
				{
					base.ErrorString = "选择的管件要与风管匹配！";
					return result;
				}
				if ((this.fittingStyle == ConnectDuctStyle.RightAngleTeeRound || this.fittingStyle == ConnectDuctStyle.SlantingTeeRound || this.fittingStyle == ConnectDuctStyle.CommonTeeRound) && ductShape != 0)
				{
					base.ErrorString = "选择的管件要与风管匹配！";
					return result;
				}
				list = connectDuctManage.GetFittingNames(this.fittingStyle);
				ConnectDuctStyle fittingStyle = this.fittingStyle;
				switch (fittingStyle)
				{
				case ConnectDuctStyle.TShapeTee:
				case ConnectDuctStyle.RoundAngleTee:
				case ConnectDuctStyle.RightAngleTee:
				case ConnectDuctStyle.IntubationTee:
				case ConnectDuctStyle.YShapeTee:
				{
					ConnAlignment connAlignment = ConnAlignmentFactory.GetConnAlignment(this.fittingStyle, base.UIDoc.Document);
					if (connAlignment != null)
					{
						result = connAlignment.connectAlignment(base.SelectedDucts, base.SelectedPoints);
						base.ErrorString = connAlignment.ErrorString;
						goto IL_255;
					}
					goto IL_255;
				}
				case ConnectDuctStyle.RReduceAngleTee:
					goto IL_255;
				case ConnectDuctStyle.CommonTee:
					break;
				default:
					if (fittingStyle - ConnectDuctStyle.RightAngleTeeRound > 1)
					{
						if (fittingStyle != ConnectDuctStyle.CommonTeeRound)
						{
							goto IL_255;
						}
					}
					else
					{
						if (YJKRevitTools.IsRiserDuct(base.SelectedDucts[2]))
						{
							familyInstance = hyductConnect.NewTeeFittingHT(base.SelectedDucts, base.SelectedPoints, list, ref empty);
							base.ErrorString = empty;
							goto IL_255;
						}
						familyInstance = hyductConnect.NewTeeFittingT(base.SelectedDucts, base.SelectedPoints, list, ref empty);
						base.ErrorString = empty;
						goto IL_255;
					}
					break;
				}
				familyInstance = hyductConnect.NewTeeFittingY(base.SelectedDucts, base.SelectedPoints, list, ref empty);
				base.ErrorString = empty;
				IL_255:
				if ((familyInstance != null && list != null) || list == null)
				{
					result = true;
					base.UIDoc.Document.Regenerate();
				}
				return result;
			}
		}

		private bool connPointSel;
	}
}
